Inside Macintosh: QuickTime Components

Previous | Chapter Top | Chapter Contents | Next

Playing Movies

The following sample code demonstrates how to use the standard movie controller component to play a movie. The GetMovie function prompts the user to select a movie file and then get a movie out of it. It then opens the movie and allows the user to play it.

Listing 1 Playing a movie with a movie controller component

MovieController         gController;
WindowPtr               gWindow;
Rect                    windowRect;
Movie                   gMovie;
Boolean                 gDone;
OSErr                   gErr;
ComponentResult         gCErr;
Boolean                 gResult;
EventRecord             gTheEvent;
WindowPtr               whichWindow;
short                   part;
pascal Movie        GetMovie(void);
pascal Movie        GetMovie(void)
{
    OSErr                   err;
    SFTypeList              typeList;
    StandardFileReply reply;
    Movie                   aMovie;
    short                   movieResFile;
    short                   movieResID;
    Str255                  movieName;
    Boolean                 wasChanged;
    aMovie = nil;
    typeList[0] = MovieFileType;
    StandardGetFilePreview ( (FileFilterProcPtr)nil, 1,
                                    typeList, &reply);
    if (reply.sfGood) {
        err = OpenMovieFile (&reply.sfFile, &movieResFile,
                                     fsRdPerm);
        if (err == noErr) {
            movieResID = 0;
            err = NewMovieFromFile (&aMovie, movieResFile,
                                             &movieResID,
                                             movieName,
                                             newMovieActive,
                                             &wasChanged);
            err = CloseMovieFile (movieResFile);
        }
    }
    return aMovie;
}
void main(void);
void main(void)
{
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);
    gErr = EnterMovies();
    SetRect (&windowRect, 100, 100, 200, 200);
    gWindow = NewCWindow (nil,
                    &windowRect,
                    "\pMovie",
                    false,
                    noGrowDocProc,
                    (WindowPtr)-1,
                    true,
                    0);
    SetPort (gWindow);
    gMovie = GetMovie();
    if (gMovie != nil) {
        SetRect(&windowRect, 0, 0, 100, 100);
        gController = NewMovieController (gMovie, &windowRect,
                                                     mcTopLeftMovie);
        if (gController != nil) {
            gCErr = MCGetControllerBoundsRect (gController,
                                                                 &windowRect);
            SizeWindow (gWindow, windowRect.right, windowRect.bottom,
                             true);
            ShowWindow (gWindow);
            gCErr = MCDoAction (gController, mcActionSetKeysEnabled,
                                        (Ptr)true);
            gDone = false;
            while (! gDone) {
                gResult = GetNextEvent (everyEvent, &gTheEvent);
                if (MCIsPlayerEvent (gController, &gTheEvent) == 0) {
                    switch (gTheEvent.what) {
                        case updateEvt:
                            whichWindow = (WindowPtr)gTheEvent.message;
                            BeginUpdate (whichWindow);
                            EraseRect (&(*whichWindow).portRect);
                            EndUpdate (whichWindow);
                            break;                      
                        case mouseDown:
                            part = FindWindow (gTheEvent.where,
                                                     &whichWindow);
                            if (whichWindow == gWindow) {
                                switch (part) {
                                    case inGoAway:
                                        gDone = TrackGoAway (whichWindow,
                                                        gTheEvent.where);
                                        break;                                  
                                    case inDrag:
                                        DragWindow (whichWindow,
                                                    gTheEvent.where,
                                                    &(qd.screenBits.bounds) );
                                        break;
                                }
                            }
                    }
                }
            }
            DisposeMovieController(gController);
        }
        DisposeMovie(gMovie);
    }
    DisposeWindow(gWindow);
}

© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next